<?php

namespace backend\models;

use common\service\PubService;
use Yii;

/**
 * This is the model class for table "baselogger".
 *
 * @property int $LogId 日志标识
 * @property int $LogType 日志类别
 * @property string $LogLogic
 * @property string $LogDescribe 日志描述
 * @property string $LogURL 发生地址
 * @property string $LogDate 记录时间
 * @property string $LogIP 记录地址
 * @property int $MasterId 操作人员
 */
class Baselogger extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'baselogger';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['LogType', 'MasterId'], 'integer'],
            [['LogLogic', 'MasterId'], 'required'],
            [['LogDate'], 'safe'],
            [['LogLogic'], 'string', 'max' => 50],
            [['LogDescribe'], 'string', 'max' => 512],
            [['LogURL'], 'string', 'max' => 256],
            [['LogIP'], 'string', 'max' => 15],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'LogId' => 'Log ID',
            'LogType' => 'Log Type',
            'LogLogic' => 'Log Logic',
            'LogDescribe' => 'Log Describe',
            'LogURL' => 'Log Url',
            'LogDate' => 'Log Date',
            'LogIP' => 'Log Ip',
            'MasterId' => 'Master ID',
        ];
    }


    public function getLogData($post, $page, $pageSize)
    {
        $where = 1;
        $offset = max(0, ($page-1))*$pageSize;
        if (isset($post['s']) && !empty($post['s'])){
            $s = $post['s'].' 00:00:00';
            $where .= " and l.LogDate >= '{$s}' ";
        }
        if (isset($post['f']) && !empty($post['f'])){
            $f = $post['f'].' 23:59:59';
            $where .= " and l.LogDate <= '{$f}' ";
        }
        $sql = "select *
                from baselogger as l 
                left join basemaster as m on m.MasterId = l.MasterId 
                where $where
                order by l.LogDate desc limit $pageSize offset $offset";
        $data = yii::$app->db->createCommand($sql)->queryAll();
        foreach ($data as $key=>$datum) {
            $ipsql = "SELECT * FROM baseip WHERE INET_ATON('".$datum['LogIP']."') BETWEEN ip_start_num AND ip_end_num LIMIT 1";
            $query = yii::$app->db->createCommand($ipsql)->queryOne();
            $data[$key]['LogIP'] = empty($query['city']) ? '<code>-- 未知IP --</code>' : $query['city'].$query['district'];
        }
        $sqla = "select *
                from baselogger as l 
                left join basemaster as m on m.MasterId = l.MasterId 
                where $where ";
        $count = yii::$app->db->createCommand($sqla)->queryAll();
        return ['data' => $data, 'limit'=> intval($pageSize) , 'page'=> intval($page), 'statusCode'=>200, 'total'=> count($count)];
    }


    public function deleteData($post)
    {
        $where = 1;
        if (isset($post['s']) && !empty($post['s'])){
            $s = $post['s'].' 00:00:00';
            $where .= " and LogDate >= '{$s}' ";
        }
        if (isset($post['f']) && !empty($post['f'])){
            $f = $post['f'].' 23:59:59';
            $where .= " and LogDate <= '{$f}' ";
        }
        $sql = "delete from baselogger where $where ";
        $result = yii::$app->db->createCommand($sql)->execute();
        if ($result){
            return ['statusCode'=>200, 'message'=>'已成功清理日志记录'];
        }
        return ['statusCode'=>300, 'message'=>'清理日志记录信息失败'];
    }
}
